//
//  AssistantRightTableViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/4/29.
//

#import "AssistantRightTableViewCell.h"

@implementation AssistantRightTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
//    self.contentView.backgroundColor = UIColor.redColor;
    [self.contentView addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(48);
        make.right.equalTo(self.contentView.mas_right).offset(-16);
    }];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 20;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = SQHexColor(@"#000000", 0.5);
    [self.contentView addSubview:bgView];
    
    [self.contentView addSubview:self.messageTextView];
    [self.messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView).offset(8);
        make.right.equalTo(self.avatarImageView.mas_left).offset(-18);
    }];
    WS(weakSelf);
    self.messageTextView.showTextMenu = ^(CGRect startRect, CGRect endRect, NSRange selectRange, BOOL isSelectAll) {
        [weakSelf showMenuWithStartRect:startRect endRect:endRect isAll:isSelectAll];
    };
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.messageTextView).insets(UIEdgeInsetsMake(-6, -10, 0, -6));
    }];

    self.FS_cellBottomView = self.messageTextView;
}
- (void)setModel:(MessageModel *)model{
    _model = model;
    [self.avatarImageView sd_setImageWithURL:SQURLImage([UserModel share].portrait) placeholderImage:SQGetImage(@"上传头像")];
    self.messageTextView.text = model.content;
    [self.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.cellSize.width);
        make.height.mas_equalTo(model.cellSize.height);
    }];
    [self.contentView layoutIfNeeded];
}
- (void)showMenuWithStartRect:(CGRect)startRect endRect:(CGRect)endRect isAll:(BOOL)isAll
{
    // 显示菜单栏
    SJPopMenuItem *copy = [SJPopMenuItem itemWithType:SJPopMenuItemCopy];
    SJPopMenuItem *selectAll = [SJPopMenuItem itemWithType:SJPopMenuItemSelectAll];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:copy];//复制
    if (!isAll) {
        [items addObject:selectAll]; //全选
    }
    SJPopMenu *menu = [SJPopMenu menu];
    if (isAll) {
        [menu showBy:self.messageTextView withItems:items];
    } else {
        [menu showBy:self.messageTextView startRect:startRect endRect:endRect withItems:items];
    }
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemCopy://复制
                [weakSelf coppy];
                break;
            case SJPopMenuItemWithdraw: //撤回
                [weakSelf recall];
                break;
            case SJPopMenuItemDelete: //删除
                [weakSelf remove];
                break;
            case SJPopMenuItemSelectAll: //全选
                [weakSelf selectAllText];
                break;
            default:
                break;
        }
    };
}

- (void)coppy
{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = [_model.content substringWithRange:self.messageTextView.selectedRange];
      if (pab == nil) {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制失败")];
      } else {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制成功")];
      }
NSLog(@"%s", __func__);
}

- (void)recall
{
NSLog(@"%s", __func__);
}

- (void)remove
{
NSLog(@"%s", __func__);
}
 

- (void)selectAllText
{
   NSLog(@"%s", __func__);
  self.messageTextView.selectedRange = NSMakeRange(0, self.messageTextView.text.length);
}

- (UIImageView *)avatarImageView{
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc]init];
        _avatarImageView.clipsToBounds = YES;
        _avatarImageView.layer.cornerRadius = 24;
        _avatarImageView.layer.masksToBounds = YES;
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _avatarImageView;
}

- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_MEDIUM(15);
        _messageLabel.textColor = SQColor333333;
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
        _messageLabel.lineBreakMode = NSLineBreakByClipping;
     }
    return _messageLabel;
}
 
- (SJCustomSelectTextView *)messageTextView{
    if (!_messageTextView) {
        _messageTextView = [[SJCustomSelectTextView alloc]init];
        _messageTextView.backgroundColor = UIColor.clearColor;
//        _messageTextView.layer.cornerRadius = 20;
//        _messageTextView.layer.masksToBounds = YES;
        _messageTextView.font = FONT_MEDIUM(15);
        _messageTextView.textColor = UIColor.whiteColor;
        _messageTextView.editable = NO;
        _messageTextView.selectable = YES;
//        _messageTextView.userInteractionEnabled = NO;
    }
    return _messageTextView;
}

@end
